package cn.demengsite.controller;

import cn.demengsite.dao.ProductDAO;
import cn.demengsite.model.BaseInfo;
import cn.demengsite.model.Product;
import cn.demengsite.model.ProductImageAttr;
import cn.demengsite.service.ProductService;
import com.alibaba.fastjson.JSON;
import com.aliyun.oss.OSSClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.ByteArrayInputStream;
import java.util.logging.Logger;

@Controller
public class ProductController {

    protected   final Logger logger = Logger.getLogger(ProductController.class.getName());

    @Autowired
    private ProductDAO productDAO;

    @Autowired
    private ProductService productService;

    @GetMapping("/product/getProductBybarCode/{barCode}")
    @ResponseBody
    public String getProductBybarCode(@PathVariable String barCode){

        BaseInfo product = productService.getProductByBarCode(barCode);

        return JSON.toJSONString(product.getObject());

    }
    @GetMapping("/product/getProductImageByProductId/{productId}")
    @ResponseBody
    public String getProductImageByProductId(@PathVariable String productId){

        BaseInfo productImages = productService.getProductImageByProductId(productId);

        return JSON.toJSONString(productImages.getObject());

    }
    @GetMapping("/product/deleteProductImageById/{id}")
    @ResponseBody
    public String deleteProductImageById(@PathVariable String id, @RequestParam(required = false) String attrUrl){

        System.out.println(attrUrl);

        BaseInfo productImages = productService.deleteProductImageById(id);
        OSSClient ossClient = new OSSClient("oss-cn-beijing.aliyuncs.com","LTAInSgk4S1HKvtC","oB34tutgBlGP01RqGmPc891NitgNZ5");
        ossClient.deleteObject("light-blue",attrUrl);

        ossClient.shutdown();

        return JSON.toJSONString(productImages.getObject());

    }
    @GetMapping("/product/setProductImageByProductId/{productId}")
    @ResponseBody
    public String setProductImageByProductId(@PathVariable String productId,@RequestParam String imageUrl){

        Product product = new Product();
        product.setProductId(productId);
        product.setProductImag(imageUrl);
        int productImages = productDAO.updateProductByProductId(product);

        return JSON.toJSONString(productImages);

    }
    @GetMapping("/product/updateImageOrder")
    @ResponseBody
    public String updateImageOrder(@RequestParam(required = false) int id,
                                   @RequestParam(required = false) int orderNum){

        ProductImageAttr imageAttr = new ProductImageAttr();
        imageAttr.setId(id);
        imageAttr.setOrderNum(orderNum);
        BaseInfo productImages = productService.updateProductImage(imageAttr);
        return JSON.toJSONString(productImages.getObject());

    }

}
